package com.dex4d.ui.qr.model;

import com.dex4d.model.User;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

/**
 * 作者    LiQingyi
 * 时间    2018/11/20 14:28
 * 文件    dex4d-android
 * 描述
 */
public class QRRequest {

    /**
     * appname : hello
     * address : 0x123456
     * nonce : x2zDck0tbswC9y0psAU5
     * confirm : true
     */

    public String appname;
    public String address;
    public String nonce;
    public String confirm;
    public String txdata;
    public String sign;

    private QRRequest() {
    }

    /**
     *
     * 创建登录请求
     * @param url
     * @param isLogin
     * @return
     */
    public static QRRequest createLoginRequest(String url, boolean isLogin) {
        QRRequest request = new QRRequest();
        request.confirm = Boolean.toString(isLogin);
        request.address = User.getInstance().getAddress();
        try {
            URLParser parser = new URLParser(url);
            request.appname = parser.getParam("appname");
            request.nonce = parser.getParam("nonce");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            request.appname = "";
            request.nonce = "";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            request.appname = "";
            request.nonce = "";
        }
        return request;
    }

    /**
     * 支付请求
     * 还需要增加 sign txdata，在数据处理过程中实现
     * @param url
     * @param isPay
     * @return
     */
    public static QRRequest createPayRequest(String url, boolean isPay) {
        QRRequest request = new QRRequest();
        request.confirm = Boolean.toString(isPay);
        try {
            URLParser parser = new URLParser(url);
            request.appname = parser.getParam("appname");
            request.address = parser.getParam("address");
            request.nonce = parser.getParam("nonce");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            request.appname = "";
            request.address = "";
            request.nonce = "";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            request.appname = "";
            request.address = "";
            request.nonce = "";
        }
        return request;
    }

}
